﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;
using System.Collections;
using System.Data;

namespace GMAC.ReportingSystem.DataAccess
{
    //--------------------------------------------------------
    /// <summary>
    /// Ricardo Recaredo (23/03/2011)
    /// 
    /// DAO for CallReasonByAgent
    /// 
    /// </summary>
    //--------------------------------------------------------

    public class CallReasonByAgentDataAccess : BaseDataAccess<Business.Entities.CallReasonByAgent>
    {
        #region Overrides for BaseDataAccess

        public override int Insert(Business.Entities.CallReasonByAgent entityToInsert)
        {
            throw new NotImplementedException();
        }

        //-----------------------------------------------------

        public override Business.Entities.CallReasonByAgent Get(object id)
        {
            throw new NotImplementedException();
        }

        //-----------------------------------------------------

        protected override GMAC.ReportingSystem.Business.Entities.CallReasonByAgent ReadRow(System.Data.DataRow row)
        {
            CallReasonByAgent callReasonByAgent = null;

            if (row != null)
            {
                try
                {
                    callReasonByAgent = new CallReasonByAgent();

                    callReasonByAgent.AccountType   = new Product    { AccountTypeID = (int)row["AccountTypeID"] };
                    callReasonByAgent.Agent         = new Agent      { ID            = (int)row["AgentId"]       };
                    callReasonByAgent.CallReason    = new CallReason { ID            = (int)row["CallReasonId"]  };

                    callReasonByAgent.CallDate      = (DateTime)row["CallReasonId"];
                    callReasonByAgent.NumberOfCalls = (int)     row["NumberOfCalls"];
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return callReasonByAgent;
        }

        //-----------------------------------------------------

        protected GMAC.ReportingSystem.Business.Entities.CallReasonByAgent ReadRowExtended(System.Data.DataRow row)
        {
            CallReasonByAgent callReasonByAgent = null;

            if (row != null)
            {
                try
                {
                    callReasonByAgent = new CallReasonByAgent();

                    callReasonByAgent.AccountType   = new Product    { AccountTypeID = (int)row["AccountTypeID"] };
                    callReasonByAgent.Agent         = new Agent      { ID = (int)row["AgentId"], FirstName = row["FirstName"].ToString(), LastName = row["LastName"].ToString() };
                    callReasonByAgent.CallReason    = new CallReason { ID = (int)row["CallReasonId"], Description = row["CallReasonName"].ToString() };
                    callReasonByAgent.CallDate      = (DateTime)row["dDate"];

                    callReasonByAgent.NumberOfCalls = int.Parse(row["NumberOfCalls"].ToString());
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return callReasonByAgent;
        }

        //-----------------------------------------------------

        protected override List<GMAC.ReportingSystem.Business.Entities.CallReasonByAgent> ReadTable(System.Data.DataTable table)
        {
            try
            {
                CallReasonByAgentCollection callReasonByAgentList = new CallReasonByAgentCollection();

                CallReasonByAgent callReasonByAgent;
                
                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        callReasonByAgent = ReadRow(row);

                        if (callReasonByAgent != null)
                        {
                            callReasonByAgentList.Add(callReasonByAgent);
                        }
                    }
                }
                return callReasonByAgentList;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //-----------------------------------------------------

        protected List<GMAC.ReportingSystem.Business.Entities.CallReasonByAgent> ReadTableExtended(System.Data.DataTable table)
        {
            try
            {
                CallReasonByAgentCollection callReasonByAgentList = new CallReasonByAgentCollection();

                CallReasonByAgent callReasonByAgent;

                if (table != null && table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        callReasonByAgent = ReadRowExtended(row);

                        if (callReasonByAgent != null)
                        {
                            callReasonByAgentList.Add(callReasonByAgent);
                        }
                    }
                }
                return callReasonByAgentList;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //-----------------------------------------------------

        public override int Update(GMAC.ReportingSystem.Business.Entities.CallReasonByAgent entityToUpdate)
        {
            throw new NotImplementedException();
        }

        //-----------------------------------------------------

        #endregion

        ////////////////////////////////////////////////////////////////

        #region Public Methods

        public CallReasonByAgentCollection GetList()
        {
            throw new NotImplementedException();
        }

        //-----------------------------------------------------

        public bool LoadMultipleCallReasonsByAgent(CallReasonByAgentCollection callReasonByAgentCollection, int userId)
        {
            ArrayList parameters = new ArrayList();
            DataTable dt         = new DataTable();

            dt.Columns.Add("AccountTypeID");
            dt.Columns.Add("CallReasonID");
            dt.Columns.Add("AgentID");
            dt.Columns.Add("dDate");
            dt.Columns.Add("Calls");

            DataRow row;

            foreach (Business.Entities.CallReasonByAgent value in callReasonByAgentCollection)
            {
                row                  = dt.NewRow();
                row["AccountTypeID"] = value.AccountType.AccountTypeID;
                row["CallReasonID"]  = value.CallReason.ID;
                row["AgentID"]       = value.Agent.ID;
                row["dDate"]         = value.CallDate.Year + "-" + value.CallDate.Month + "-" + value.CallDate.Day;
                row["Calls"]         = value.NumberOfCalls;

                dt.Rows.Add(row);
            }

            parameters.Add(dt);
            parameters.Add(userId);
            
            int result = (int)DataAccessComponent.ExecuteOperation("procUpdateCallReasonsByAgent", parameters);

            if (result > 0)
            {
                return true;
            }

            return false;
        }

        //-----------------------------------------------------

        public CallReasonByAgentCollection GetListByDateAndProduct(DateTime dDate, int accountTypeId)
        {
            ArrayList parameters = new ArrayList { accountTypeId, dDate };

            DataTable dt = DataAccessComponent.ExecuteSelect("procListCallReasonsAgentByProduct", parameters);

            return ReadTableExtended(dt) as CallReasonByAgentCollection;
        }

        #endregion
    }
}
